package com.ruoyi.activity.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.activity.domain.TbActivityConfig;
import com.ruoyi.activity.domain.TbMerchant;
import com.ruoyi.activity.domain.TbUser;
import com.ruoyi.activity.domain.vo.TbUserCouponVO;
import com.ruoyi.activity.service.ITbMerchantService;
import com.ruoyi.activity.service.ITbUserService;
import com.ruoyi.common.annotation.Anonymous;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.activity.domain.TbUserCoupon;
import com.ruoyi.activity.service.ITbUserCouponService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户领卷记录Controller
 * 
 * @author jll
 * @date 2025-06-13
 */
@RestController
@RequestMapping("/ruoyi-activity/coupon")
public class TbUserCouponController extends BaseController
{
    @Autowired
    private ITbUserCouponService tbUserCouponService;
    @Autowired
    private ITbUserService  tbUserService;
    @Autowired
    private ITbMerchantService tbMerchantService;

    /**
     * 查询用户领卷记录列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-activity:coupon:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbUserCouponVO tbUserCouponVO)
    {
        startPage();
        List<TbUserCouponVO> list = tbUserCouponService.selectTbUserCouponList(tbUserCouponVO);
        return getDataTable(list);
    }

    /**
     * 导出用户领卷记录列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-activity:coupon:export')")
    @Log(title = "用户领卷记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbUserCouponVO tbUserCouponVO)
    {
        List<TbUserCouponVO> list = tbUserCouponService.selectTbUserCouponList(tbUserCouponVO);
        ExcelUtil<TbUserCouponVO> util = new ExcelUtil<TbUserCouponVO>(TbUserCouponVO.class);
        util.exportExcel(response, list, "用户领卷记录数据");
    }

    /**
     * 获取用户领卷记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-activity:coupon:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbUserCouponService.selectTbUserCouponById(id));
    }

    /**
     * 新增用户领卷记录
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-activity:coupon:add')")
    @Log(title = "用户领卷记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbUserCoupon tbUserCoupon)
    {
        return toAjax(tbUserCouponService.insertTbUserCoupon(tbUserCoupon));
    }

    /**
     * 修改用户领卷记录
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-activity:coupon:edit')")
    @Log(title = "用户领卷记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbUserCoupon tbUserCoupon)
    {
        return toAjax(tbUserCouponService.updateTbUserCoupon(tbUserCoupon));
    }

    /**
     * 删除用户领卷记录
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-activity:coupon:remove')")
    @Log(title = "用户领卷记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbUserCouponService.deleteTbUserCouponByIds(ids));
    }

    @Anonymous
    @PostMapping("/writeOffCode")
    @ApiOperation("核销二维码")
    public AjaxResult writeOffCode(@RequestBody TbUserCoupon tbUserCoupon) {
        if (ObjectUtils.isEmpty(tbUserCoupon.getMerchantId())) {
            return AjaxResult.error("商家id不能为空");
        }
        if (ObjectUtils.isEmpty(tbUserCoupon.getCouponCode())) {
            return AjaxResult.error("核销码不能为空");
        }
        TbMerchant tbMerchant = tbMerchantService.selectTbMerchantById(tbUserCoupon.getMerchantId());
        if (ObjectUtils.isEmpty(tbMerchant)) {
            return AjaxResult.error("商家不存在");
        }
        TbUserCoupon selectTbUserCouponByCouponCode = tbUserCouponService.selectTbUserCouponByCouponCode(tbUserCoupon.getCouponCode());
        if (selectTbUserCouponByCouponCode == null) {
            return AjaxResult.error("核销码不存在");
        }
        if (selectTbUserCouponByCouponCode.getStatus().equals("1")){
            return AjaxResult.error("核销码已使用");
        }
        selectTbUserCouponByCouponCode.setStatus("1");
        selectTbUserCouponByCouponCode.setMerchantId(tbUserCoupon.getMerchantId());
        tbUserCouponService.updateTbUserCoupon(selectTbUserCouponByCouponCode);
        TbUser tbUser = tbUserService.selectTbUserById(selectTbUserCouponByCouponCode.getUserId());
        return AjaxResult.success(tbUser);
    }
}
